package com.gcstorage.tvdev.base;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gcstorage.tvdev.R;
import com.gcstorage.tvdev.view.LoadingDialog;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;


/**
 * Description: activity基类
 * Author: Sanpark
 * Date: 2020/5/20 14:06
 */
public abstract class RxBaseActivity extends RxAppCompatActivity {
    public RxBaseActivity mActivity;
    public LoadingDialog loadingDialog;

    public int getLayoutId() {
        return -1;
    }

    public View getRootView() {
        return null;
    }

    public abstract void initView();

    public abstract void initData();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        if (useImmersionBar()) {
//            ImmersionBar.with(this).transparentBar().transparentNavigationBar().transparentBar()
//                    .statusBarDarkFont(true).statusBarColor(R.color.transparent).init();
        }
        initIntent();
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
        } else if (getRootView() != null) {
            setContentView(getRootView());
        } else {
            throw new IllegalArgumentException("error layoutId");
        }
//        if (useEventBus()) EventBus.getDefault().register(this);
        initBaseView();
        initView();
        if (savedInstanceState != null) {
            initSave(savedInstanceState);
        }
        initData();
    }

    protected void initSave(Bundle saveBundle) {

    }

    public void initBaseView() {
        ImageView iv_back = findViewById(R.id.iv_back);
        ImageView iv_action = findViewById(R.id.iv_action);
        TextView tv_title = findViewById(R.id.tv_title);
        TextView tv_action = findViewById(R.id.tv_action);
        if (iv_back != null) {
            iv_back.setVisibility(hasBackButton() ? View.VISIBLE : View.GONE);
            iv_back.setOnClickListener(v -> {
                onBackPressed();
            });
        }
        if (tv_title != null) {
            tv_title.setText(initTitleName());
        }
        if (tv_action != null) {
            tv_action.setVisibility(hasActionText() ? View.VISIBLE : View.GONE);
            tv_action.setText(getActionText());
        }
        if (iv_action != null) {
            iv_action.setVisibility(hasActionButton() ? View.VISIBLE : View.GONE);
            iv_action.setImageResource(getActionResources());
        }
    }

    protected String initTitleName() {
        return "";
    }

    public void initIntent() {
    }

    //当前活动是否使用EventBus,默认不使用
    public boolean useEventBus() {
        return false;
    }

    //是否有返回键 默认有
    public boolean hasBackButton() {
        return true;
    }

    //是否有action键 默认无
    public boolean hasActionButton() {
        return false;
    }

    public boolean getFitsSystemWindow() {
        return true;
    }

    public boolean useImmersionBar() {
        return true;
    }

    public int getActionResources() {
        return R.drawable.ic_default;
    }

    public boolean hasActionText() {
        return false;
    }

    public String getActionText() {
        return "确定";
    }

//    //占位空响应
//    @Subscribe
//    public void placeholderEvent() {
//
//    }

    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
    }

    public void showLoadingDialog(String message, boolean cancelable) {
//        new XPopup.Builder(this)
//                .dismissOnBackPressed(cancelable)
//                .dismissOnTouchOutside(cancelable)
//                .asLoading(message)
//                .show();
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mActivity);
            loadingDialog.setCancelable(cancelable);
        }
        loadingDialog.show();
    }

    public void showLoadingDialog(boolean cancelable) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mActivity);
            loadingDialog.setCancelable(cancelable);
        }
        loadingDialog.show();
    }

    public void showLoadingDialog() {
        showLoadingDialog(true);
    }

    public void closeLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

//    @Override
//    public void startActivity(Intent intent, Bundle bundle) {
//        if (intent != null && ACTION_VIEW.equals(intent.getAction())) {
//            Uri uri = intent.getData();
//            if (uri != null && ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme()))) {
//            }
//        }
//        super.startActivity(intent, bundle);
//    }
}


